const Router = require('koa-router')
const router = new Router()
const callCloudDB = require('../utils/callCloudDB.js')
const cloudStorage = require('../utils/cloudStorage.js')
// 获取列表

router.get('/list', async (ctx) => {
    // 默认http api获取10条数据
    const query = `db.collection('book').get()`
    const res = await callCloudDB(ctx, 'databasequery', query)
    const data = res.data
    //处理数据
    let returnData = []
    for (let i = 0, len = data.length; i < len; i++) {
        returnData.push({
            _id: JSON.parse(data[i])._id,
            username: JSON.parse(data[i]).username,
            phone: JSON.parse(data[i]).phone,
            time: JSON.parse(data[i]).enterdate,
            count: JSON.parse(data[i]).count,
            bookitem: JSON.parse(data[i]).bookitem,
            shop: JSON.parse(data[i]).shop,
            message: JSON.parse(data[i]).message
        })
    }
    ctx.body = {
        code: 20000,
        data: returnData
    }

})


router.get('/del', async (ctx, next) => {
    const params = ctx.request.query
    // 删除云数据库中的内容
    const query = `db.collection('book').doc('${params._id}').remove()`
    const delDBRes = await callCloudDB(ctx, 'databasedelete', query)

    // 删除云存储中的文件

    ctx.body = {
        code: 20000,
        data: {
            delDBRes

        }
    }
})

module.exports = router